package com.shenmazong.servertalkadmin.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shenmazong.pojo.TbSeller;
import com.shenmazong.pojo.TbStudent;
import com.shenmazong.pojo.TbSubject;
import com.shenmazong.pojo.TbTeacher;
import com.shenmazong.servertalkadmin.mapper.TbStudentMapper;
import com.shenmazong.servertalkadmin.service.TbStudentService;
import com.shenmazong.utils.JwtTokenUtils;
import com.shenmazong.utils.ResponseResult;
import com.shenmazong.utils.StringTools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author 王大勇
 * @Date 2020-9-11
 **/
@Service
@Slf4j
public class TbStudentServiceImpl implements TbStudentService {

    @Autowired
    private TbStudentMapper studentMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResponseResult addStudentUser(String studentName, String nickName, String studentMobile, Long studentFee) {
        ResponseResult result=ResponseResult.SUCCESS();
        HashMap<String,Object> resultMap=new HashMap<>();
        //判断学生是否存在
        TbStudent student = studentMapper.selectOne(new QueryWrapper<TbStudent>()
                .eq("user_name", studentName)
                .last("limit 1"));
        if(student!=null){
            result.setFailMessage("学生已存在");
            return result;
        }
        //添加学生信息
        student = new TbStudent();
        student.setUserName(studentName);
        student.setMobile(studentMobile);
        student.setStudentFee(studentFee);
        //设置学生昵称
        student.setNickName(nickName);
        //生成字符串
        String salt= StringTools.getRandomString(32);
        student.setPassSalt(salt);
        //设置密码
        String password="123456"+"."+salt;
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        student.setUserPass(md5Password);
        studentMapper.insert(student);
        //判断是否添加成功
        if(student.getId() != null){
            student.setUserPass("");
            student.setPassSalt("");
            resultMap.put("student",student);
            result.setResult(resultMap);
            return result;
        }
        //添加失败
        result.setFailMessage("学生添加失败");
        return result;
    }

    @Override
    public ResponseResult delStudentUser(Integer studentId) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //--1 判断学生是否存在
        TbStudent student = studentMapper.selectById(studentId);
        if(student==null) {
            result.setFailMessage("学生不存在");
            return result;
        }
        //判断是否删除
        if(!student.getStatus().equals(0)) {
            result.setFailMessage("学生已删除");
            return result;
        }
        //--2 设置删除标志
        student.setStatus(1);
        studentMapper.updateById(student);
        student.setUserPass("");
        student.setPassSalt("");
        resultMap.put("student", student);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult setStudentPass(Integer studentId, String studentPass) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //--1 判断学生是否存在
        TbStudent student = studentMapper.selectById(studentId);
        if(student == null) {
            result.setFailMessage("学生不存在");
            return result;
        }
        if(!student.getStatus().equals(0)) {
            result.setFailMessage("学生已删除");
            return result;
        }
        //-- 验证旧密码
        //TODO

        //--2 修改密码
        // 生成密码(123456.oieuroqeuro)
        String password = studentPass + "." + student.getPassSalt();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        log.info("pass:"+md5Password);
        student.setUserPass(md5Password);
        studentMapper.updateById(student);

        student.setUserPass("");
        student.setPassSalt("");
        resultMap.put("student", student);
        result.setResult(resultMap);

        return result;
    }

    @Override
    public ResponseResult doStudentLogin(String studentName, String studentPass) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        //--1 查询用户
        TbStudent student = studentMapper.selectOne(new QueryWrapper<TbStudent>()
                .eq("user_name", studentName)
                .eq("status", 0)
                .last("limit 1"));
        if(student == null) {
            result.setFailMessage("学生不存在");
            return result;
        }

        //--2 判断密码
        String password = studentPass + "." + student.getPassSalt();
        String md5Password = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!student.getUserPass().equals(md5Password)) {
            result.setFailMessage("学生密码不正确");
            return result;
        }
        //--3 生成token
        String token = JwtTokenUtils.makeToken(student.getId(), student.getUserName(), "student");

        student.setUserPass("");
        student.setPassSalt("");
        resultMap.put("student", student);
        resultMap.put("token", token);
        result.setResult(resultMap);

        //--4 单点登录
        String key = "student_token_" + student.getId();
        redisTemplate.opsForValue().set(key, token, 1, TimeUnit.DAYS);
        return result;
    }

    @Override
    public ResponseResult setStudentInfo(Integer studentId, String nickName, String studentMobile, Long studentFee) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //-- 检查用户是否存在
        TbStudent student = studentMapper.selectById(studentId);
        if(student==null) {
            result.setFailMessage("学生不存在");
            return result;
        }

        if(!student.getStatus().equals(0)) {
            result.setFailMessage("学生已删除");
            return result;
        }

        //--1 用户名
        student = new TbStudent();
        student.setId(studentId);
        student.setNickName(nickName);
        student.setMobile(studentMobile);
        student.setStudentFee(studentFee);
        studentMapper.updateById(student);

        resultMap.put("student", student);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getStudentInfo(Integer studentId) {
        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();
        //--1 查询学生是否存在
        TbStudent student = studentMapper.selectOne(new QueryWrapper<TbStudent>()
                .eq("id", studentId)
                .eq("status", 0)
                .last("limit 1"));
        if(student == null) {
            result.setFailMessage("学生不存在");
            return result;
        }
        //是否删除
        if(!student.getStatus().equals(0)) {
            result.setFailMessage("学生已删除");
            return result;
        }
        resultMap.put("student", student);
        result.setResult(resultMap);
        return result;
    }

    @Override
    public ResponseResult getStudentByPage(Integer pageCurrent, Integer pageSize) {

        ResponseResult result = ResponseResult.SUCCESS();
        HashMap<String, Object> resultMap = new HashMap<>();

        Page<TbStudent> page = new Page<>(pageCurrent, pageSize);

        IPage<TbStudent> pages = studentMapper.selectPage(
                page,
                new QueryWrapper<TbStudent>()
                        .select(TbStudent.class, i -> !i.getColumn().equals("user_pass") && !i.getColumn().equals("pass_salt"))
                        .orderByAsc("id")
        );

        resultMap.put("pages", pages);
        result.setResult(resultMap);
        return result;
    }


}
